<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="配方名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="formulaName">
              <a-input v-model="model.formulaName" placeholder="请输入配方名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="配方卡号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fcNo">
              <a-input v-model="model.fcNo" placeholder="请输入配方卡号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableTime">
              <j-date placeholder="请选择生效日期" v-model="model.enableTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="使用日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTime">
              <j-date placeholder="请选择使用日期" v-model="model.useTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="配方时效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expiredFlag">
              <j-dict-select-tag type="list" v-model="model.expiredFlag" dictCode="formula_aging" placeholder="请选择配方时效"
                @change="handleExpiredFlagChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.expiredFlag === 1">
            <a-form-model-item label="失效日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expiredTime">
              <j-date placeholder="请选择失效日期" v-model="model.expiredTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="变更原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="changeReason">
              <a-input v-model="model.changeReason" placeholder="请输入变更原因"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="formulaType">
              <j-dict-select-tag type="list" v-model="model.formulaType" dictCode="" placeholder="请选择类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="CrutcherCMM" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="crutchercmm">
              <a-input v-model="model.crutchercmm" placeholder="请输入CrutcherCMM"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行MINo" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mino">
              <a-input v-model="model.mino" placeholder="请输入执行MINo"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行PCSCBANo" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pcscbano">
              <a-input v-model="model.pcscbano" placeholder="请输入执行PCSCBANo"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="FCSolid" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fcsolid">
              <a-input v-model="model.fcsolid" placeholder="请输入FCSolid"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-if="title !== '新增'" v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="前配" :key="refKeys[2]" :forceRender="true">
        <j-vxe-table keep-source :ref="refKeys[2]" :loading="eneFormulaPgFrontTable.loading"
          :columns="eneFormulaPgFrontTable.columns" :dataSource="eneFormulaPgFrontTable.dataSource" :maxHeight="300"
          :disabled="formDisabled" :rowNumber="true" :rowSelection="true" :toolbar="false" />
      </a-tab-pane>
      <a-tab-pane tab="后配" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table keep-source :ref="refKeys[0]" :loading="eneFormulaPgBehindTable.loading"
          :columns="eneFormulaPgBehindTable.columns" :dataSource="eneFormulaPgBehindTable.dataSource" :maxHeight="300"
          :disabled="formDisabled" :rowNumber="true" :rowSelection="true" :toolbar="false" />
      </a-tab-pane>
      <a-tab-pane tab="回溶加量" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table keep-source :ref="refKeys[1]" :loading="eneFormulaPgRepairTable.loading"
          :columns="eneFormulaPgRepairTable.columns" :dataSource="eneFormulaPgRepairTable.dataSource" :maxHeight="300"
          :disabled="formDisabled" :rowNumber="true" :rowSelection="true" :toolbar="false" />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'EneFormulaPgBaseForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        model: {
        expiredFlag: 1, // 0=长期有效，1=失效日期
        expiredTime: null, // 失效日期
      },
      rules: {
        expiredFlag: [{ required: true, message: '请选择配方时效', trigger: 'change' }],
        expiredTime: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.model.expiredFlag === 1 ) {
                callback(new Error('请选择失效日期'));
              } else {
                callback();
              }
            },
            trigger: 'change',
          },
        ],
      },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           formulaName: [
              { required: true, message: '请输入配方名称!'},
           ],
           fcNo: [
              { required: true, message: '请输入配方卡号!'},
           ],
        },
        refKeys: ['eneFormulaPgBehind', 'eneFormulaPgRepair', 'eneFormulaPgFront', ],
        tableKeys:['eneFormulaPgBehind', 'eneFormulaPgRepair', 'eneFormulaPgFront', ],
        activeKey: 'eneFormulaPgBehind',
        // 后配
        eneFormulaPgBehindTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '关联配方id',
              key: 'formulaId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '原料id',
              key: 'ingredientId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '系统',
              key: 'systemInfo',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },  
            {
              title: '加料设定',
              key: 'chargeSetting',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '说明',
              key: 'remark',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 回溶加量
        eneFormulaPgRepairTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '配方数据id',
              key: 'formulaId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 配方数据_宝洁_前配/预称重
        eneFormulaPgFrontTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '关联配方id',
              key: 'formulaId',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '品牌',
              key: 'brand',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'FC No',
              key: 'fcNo',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '配料罐重',
              key: 'ingredientTankWeight',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '料浆因子',
              key: 'slurryFactor',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'FC.Solid',
              key: 'fcSolid',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'Crutcher CMM',
              key: 'crutcherCmm',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/craft/eneFormulaPgBase/add",
          edit: "/craft/eneFormulaPgBase/edit",
          queryById: "/craft/eneFormulaPgBase/queryById",
          eneFormulaPgBehind: {
            list: '/craft/eneFormulaPgBase/queryEneFormulaPgBehindByMainId'
          },
          eneFormulaPgRepair: {
            list: '/craft/eneFormulaPgBase/queryEneFormulaPgRepairByMainId'
          },
          eneFormulaPgFront: {
            list: '/craft/eneFormulaPgBase/queryEneFormulaPgFrontByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: {
        type: String,
        default: '',
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      handleExpiredFlagChange(value) {
      if (value === 0) {
        this.model.expiredTime = null;
      }
    },
      addBefore(){
        this.eneFormulaPgBehindTable.dataSource=[]
        this.eneFormulaPgRepairTable.dataSource=[]
        this.eneFormulaPgFrontTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.eneFormulaPgBehind.list, params, this.eneFormulaPgBehindTable)
          this.requestSubTableData(this.url.eneFormulaPgRepair.list, params, this.eneFormulaPgRepairTable)
          this.requestSubTableData(this.url.eneFormulaPgFront.list, params, this.eneFormulaPgFrontTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          eneFormulaPgBehindList: allValues.tablesValue[0].tableData,
          eneFormulaPgRepairList: allValues.tablesValue[1].tableData,
          eneFormulaPgFrontList: allValues.tablesValue[2].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>